﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace CountdownTool
{
    public class Countdown
    {
        public static int LastMinuteThreshold = 2;

        private readonly SettingViewModel _setting;
        private bool _isCounting;
        private readonly Timer _countTimer;
        private bool _isPositive;

        private DateTime _preDateTime;
        private DateTime _preReportTime;
        private bool _hasReportLastMinute;

        /// <summary>
        /// 报时间隔（秒）
        /// </summary>
        public double ReportInverval { get; set; }

        public event EventHandler ReportInvervalElapsed;
        public event EventHandler ReportLastOneMinute;

        public Countdown(SettingViewModel setting)
        {
            _setting = setting;
            _countTimer = new Timer(100);
            _countTimer.Elapsed += CountTimerOnElapsed;           
        }

        private void CountTimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            var now = DateTime.Now;

            if (!IsNextSecond(now, _preDateTime))
            {
                return;
            }

            _preDateTime = now;
            if (_isPositive)
            {
                CountOneSecond();
            }
            else
            {
                CountdownOneSecond();
            }


            // 报告时间间隔
            if (ReportInverval > 0)
            {
                if ((now - _preReportTime).TotalSeconds > ReportInverval)
                {
                    _preReportTime = now;
                    ReportInvervalElapsed?.Invoke(this, EventArgs.Empty);
                }
            }

            // 报告最后时间
            if (_setting.CurrentMuniteValue < LastMinuteThreshold)
            {
                if (!_hasReportLastMinute)
                {
                    ReportLastOneMinute?.Invoke(this, EventArgs.Empty);
                    _hasReportLastMinute = true;
                }
            }

        }

        private bool IsNextSecond(DateTime now, DateTime preDateTime)
        {
            return preDateTime.AddSeconds(1).Second == now.Second;
        }

        public void Start(bool isPositive = false)
        {
            if (_isCounting)
            {
                return;
            }
            _isCounting = true;
            _isPositive = isPositive;
            _preDateTime = DateTime.Now;
            _preReportTime = _preDateTime;
            _hasReportLastMinute = false;
            _countTimer.Start();
        }

        public void Stop()
        {
            _countTimer.Stop();
            _isCounting = false;
        }

        /// <summary>
        /// 倒计时
        /// </summary>
        private void CountdownOneSecond()
        {

            if (_setting.IsOverZero)
            {
                CountOneSecond();
                return;
            }

            if (_setting.CurrentMuniteValue <= 0 && _setting.CurrentSecondValue <= 0)
            {
                CountdownToZero?.Invoke(this, EventArgs.Empty);
            }
            else
            {
                _setting.CurrentSecondValue--;
            }
        }

        /// <summary>
        /// 正计时
        /// </summary>
        private void CountOneSecond()
        {
            _setting.CurrentSecondValue++;
        }

        public event EventHandler CountdownToZero;

    }
}
